package edu.gatech.fiveminutesleft;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import edu.gatech.fiveminutesleft.config.AccountManager;

/**
 * Activity to handle regisration of new accounts.
 * 
 * @author Garret Malmquist
 */
public class RegisterActivity extends Activity {

	private TextView	status, nameField, userField, emailField, passwordField, confirmField;

	/**
	 * Standard Android method. Called when Activity gets launched without already being in stopped
	 * or paused state. Sets the layout of this activity and sets the TextView instance variables.
	 */
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.register);
		Log.d("RegisterActivity.onCreate", "RegisterActivity Created");

		status = (TextView) findViewById(R.id.registerStatusLabel);

		nameField = (TextView) findViewById(R.id.registerName);
		userField = (TextView) findViewById(R.id.registerUsername);
		emailField = (TextView) findViewById(R.id.registerEmail);
		passwordField = (TextView) findViewById(R.id.registerPassword);
		confirmField = (TextView) findViewById(R.id.registerConfirm);
	}

	/**
	 * Standard Android method. Called when this activity becomes displayed after being stopped or
	 * paused. Retrieves any preset values that may have been passed (default values for userField,
	 * emailField, and passwordField)
	 */
	public void onStart() {
		super.onStart();
		Intent n = getIntent();
		Bundle extras = n.getExtras();
		Log.d("RegisterActivity.onStart", "RegisterActivity Started");

		String info = extras.getString("userinfo");

		TextView regu = (TextView) findViewById(R.id.registerUsername);
		TextView rege = (TextView) findViewById(R.id.registerEmail);
		if (info.matches(".*?@.*?\\..*?")) { // They tried to login with email
			rege.setText(info);
		} else {
			regu.setText(info);
		}

		passwordField.setText(extras.getCharSequence("userpass"));
	}

	/**
	 * Closes this Activity, returning to the previous Activity. Called when the "Cancel" button is
	 * clicked.
	 * 
	 * @param view
	 *            the button that was clicked
	 */
	public void cancel(View view) {
		finish();
	}

	@Override
	/**
	 * Standard Android method. Case when user presses physical back button - ends activity.
	 */
	public void onBackPressed() {
		finish();
	}

	/**
	 * Standard Android method. Called when this activity stops being displayed, but not actually
	 * finished. Currently does nothing but aid debugging by logging activity flow.
	 */
	public void onStop() {
		Log.d("RegisterActivity", "Register stopped");
		super.onStop();
	}

	/**
	 * onClick method for register button. Tries to register a new account using the information in
	 * the TextViews. Collaborates with AccountManager to do this. Called when the user clicks the
	 * "Register" button. If registration is successful, log in and go to the main screen.
	 * Otherwise, display an error message.
	 * 
	 * @param view
	 *            the button clicked.
	 */
	public void register(View view) {
		Log.d("RegisterActivity.register", "Register button clicked");
		String response = AccountManager.getInstance().register(nameField.getText().toString(),
				userField.getText().toString(), passwordField.getText().toString(), confirmField.getText().toString(),
				emailField.getText().toString());

		if (response != null) {
			status.setText(response);
		} else {

			Log.d("RegisterActivity.register", userField.getText().toString());
			ListsActivity.launch(this, AccountManager.getLoggedIn(), true);

			finish();
		}
	}
}